local RSNG = RSNG
local AceOO = AceLibrary("AceOO-2.0")
local AceEvent = AceLibrary("AceEvent-2.0")
local L = AceLibrary("Ace-Locale-2.2"):new("RSNG")
local C = RSNG.Classes

local CheckInteractDistance = CheckInteractDistance
local UnitIsVisible = UnitIsVisible
local GetRealZoneText = GetRealZoneText

local Filter = AceOO.Class(C.IFilter, C.ISerializable)

function Filter.prototype:init()
	Filter.super.prototype.init(self)
end

function Filter.prototype:Filter(unit, name, class, group, dead, online, zone)
	local range
	local type = self.type
	if type == "30" then
		range = CheckInteractDistance(unit, 4)
	elseif type == "10" then
		range = CheckInteractDistance(unit, 3)
	elseif type == "100" then
		range = UnitIsVisible(unit)
	elseif type == "zone" then
		range = (GetRealZoneText() == zone)
	end
	if self.include then
		return range
	else
		return not range
	end
end

function Filter.prototype:Serialize(type, data)
	if type == "save" then
		data.include = self.include
		data.type = self.type
	elseif type == "load" then
		self.include = data.include
		self.type = data.type
	end
end

function Filter.prototype:GetOptionTable()
	local t = self.options
	if not t then
		t = {
			type = "group",
			args = {
				include = {
					type = "toggle",
					name = L["Include"],
					desc = L["Filter in or out the units based on range."],
					get = function () return self.include end,
					set = function (value) self.include = value and true or false end,
				},
				type = {
					type = "text",
					name = L["Range"],
					desc = L["Range to filter."],
					get = function () return self.type end,
					set = function (value) self.type = value and true or false end,
					validate = { "10", "30", "100", "zone" },
				},
			},
		}
		self.options = t
	end
	return t
end

RSNG:RegisterFilter(L["Range Filter"], Filter)
